package com.ls.springbootdemo.service.impl;

import com.ls.springbootdemo.mapper.UserMapper;
import com.ls.springbootdemo.model.User;
import com.ls.springbootdemo.service.TransService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author 挥之以墨
 */
@Service
public class TransServiceImpl implements TransService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private ApplicationContext applicationContext;

    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    @Override
    public void addUser(User user) {
        userMapper.insert(user);
        // 获取代理对象
        TransServiceImpl proxy = applicationContext.getBean(this.getClass());

        user.setId(1);
        user.setName("李四");
        user.setAge(20);
        proxy.updateUser(user);
    }

    @Transactional(propagation = Propagation.NEVER, rollbackFor = Exception.class)
    @Override
    public void updateUser(User user) {
        userMapper.updateById(user);
    }

}
